package appinfo.controller;

import appinfo.pojo.*;
import appinfo.pojo.vo.AppSearchVo;
import appinfo.service.*;
import appinfo.util.EmptyUtils;
import appinfo.util.PageBean;
import com.alibaba.fastjson.JSON;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @author lzfei
 **/
@RequestMapping("/manager")
@Controller
public class ManagerUserController {

    @Resource
    private ManagerUserService managerUserService;

    @RequestMapping("/main")
    public String main(){
        return "backend/main";
    }

    @RequestMapping("/login")
    public String toLogin(){
        return "backendlogin";
    }
    @RequestMapping("logout")
    public String logout(HttpSession session){
        session.removeAttribute("backendUserSession");
        return "backendlogin";
    }

    @RequestMapping("/dologin")
    public String dologin(@RequestParam String userCode , @RequestParam String userPassword, HttpServletRequest request){
        if(EmptyUtils.isEmpty(userCode)|| EmptyUtils.isEmpty(userPassword)){
            request.setAttribute("error","用户名或密码不能为空！");
        }
        BackendUser backendUser =  managerUserService.dologin(userCode,userPassword);
        if(EmptyUtils.isNotEmpty(backendUser)){
            HttpSession session = request.getSession();
            session.setAttribute("backendUserSession",backendUser);
            return "redirect:/manager/main";
        }else {
            request.setAttribute("error","用户名或密码错误");
            return "backendlogin";
        }

    }

    @Resource
    private AppInfoService appInfoService;
    @Resource
    private DataDictionaryService dataDictionaryService;
    @Resource
    private AppCategoryService appCategoryService;

    @Resource
    private AppVersionService appVersionService;

    @RequestMapping("/backend/app/categorylevellistapplist")
    @ResponseBody
    public String categoryLevelList(@RequestParam Integer pid){
        List<AppCategory> categoryLevelList = appCategoryService.findCategoryLevelList(pid);
        return JSON.toJSONString(categoryLevelList);
    }


    @RequestMapping("/backend/app/list")
    public String appList(@ModelAttribute AppSearchVo vo, HttpServletRequest request){
        vo.setPageIndex(vo.getPageIndex() == null ? 1 : vo.getPageIndex());
        vo.setPageSize(5);
        PageBean<AppInfo> pages = appInfoService.findPage(vo);
        // 所属平台列表查询
        List<DataDictionary> flatFormList =  dataDictionaryService.findFlatFormList("APP_FLATFORM");


        // 查询以及分类 categoryLevel1List
        List<AppCategory> categoryLevel1List = appCategoryService.findCategoryLevelList(null);
        // 在有一级分类的情况下 查询二级分类
        if (categoryLevel1List != null){
            List<AppCategory> categoryLevel2List = appCategoryService.findCategoryLevelList(vo.getQueryCategoryLevel1());
            // 在有二级分类的情况下 查询三级分类
            if (categoryLevel2List != null){
                List<AppCategory> categoryLevel3List = appCategoryService.findCategoryLevelList(vo.getQueryCategoryLevel2());
                request.setAttribute("categoryLevel3List",categoryLevel3List);
            }
            request.setAttribute("categoryLevel2List",categoryLevel2List);

        }

        request.setAttribute("categoryLevel1List",categoryLevel1List);
        request.setAttribute("flatFormList",flatFormList);
        request.setAttribute("pages",pages);

        // 回显
        request.setAttribute("vo",vo);
        return  "backend/applist";
    }
    @RequestMapping("/backend/app/check")
    public String check(@RequestParam String aid,@RequestParam String vid,HttpServletRequest request){
        AppInfo appInfo = appInfoService.findApp(aid);
        AppVersion appVersion =  appVersionService.findVersion(vid);
        request.setAttribute("appInfo",appInfo);
        request.setAttribute("appVersion",appVersion);
        return "backend/appcheck";
    }

    @RequestMapping("/backend/app/checksave")
    public String checkSave(@RequestParam String id,@RequestParam String status,HttpServletRequest request){
        if (status.equals("2")){
            Boolean aBoolean =  appInfoService.update(id,status);
            if(!aBoolean){
                request.setAttribute("error","审核状态修改失败请重试！");
                return "backend/appcheck";
            }
        }else if(status.equals("3")){
            Boolean aBoolean =  appInfoService.update(id,status);
            if(!aBoolean){
                request.setAttribute("error","审核状态修改失败请重试！");
                return "backend/appcheck";
            }
        }
        return  "redirect:/manager/backend/app/list";

    }


}
